package com.aispeech.task.common.data.vo.response;

import com.aispeech.task.common.data.enums.APICode;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Data;

@SuppressWarnings("unchecked")
@Data
@AllArgsConstructor(staticName = "of")
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Result<T> {
    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    public static Result ok() {
        return ok(APICode.OK.getCode(), APICode.OK.getMessage(), null);
    }

    public static <T> Result ok(T result) {
        return ok(APICode.OK.getCode(), APICode.OK.getMessage(), result);
    }

    private static <T> Result result(int code, String message, T data) {
        return new Result(code, message, data);
    }

    public static <T> Result ok(int code, String message, T data) {
        return result(code, message, data);
    }

    public static Result fail() {
        return fail(APICode.INTERNAL_ERROR);
    }

    public static Result fail(APICode err) {
        return fail(err.getCode(), err.getMessage());
    }

    public static Result fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> Result fail(int code, String message, T data) {
        return result(code, message, data);
    }
}
